package constant.milk.toilet.dialog;

import constant.milk.toilet.R;
import constant.milk.toilet.util.MilkUtil;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MilkStatsDialog extends Dialog
{
	private Context context;
	private float TEXT_SIZE;
	
	private StatsCollBank collbank = null;
	
	private View contentView;
	
	private View year;
	private View month;
	private View day;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_stats);
		
		setSettingFontSize();
		
		setFindView();
		setOnClick();
		setData();
	}
	
	private void setFindView()
	{
		contentView = findViewById(R.id.dialogStatsContentView);
		year = findViewById(R.id.dialogYearView);
		month = findViewById(R.id.dialogMonthView);
		day = findViewById(R.id.dialogDayView);
	}

	private void setOnClick()
	{
		contentView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismiss();
			}
		});
		
		year.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getStats(0);
				dismiss();
			}
		});
		
		month.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getStats(1);
				dismiss();
			}
		});
		
		day.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getStats(2);
				dismiss();
			}
		});
	}

	private void setData()
	{
		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		setGlobalFont(root);
	}

	public MilkStatsDialog(Context context, StatsCollBank collBank)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		
		this.context = context;
		this.collbank = collBank;
	}
	
	private void setSettingFontSize()
	{
		int fontSize = 9;
		
		SharedPreferences p = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int fontFlag = p.getInt(context.getString(R.string.prefFont), 1);
		
		if (fontFlag == 0) fontSize = 13;
		else if (fontFlag == 1) fontSize = 16;
		else if (fontFlag == 2) fontSize = 20;
		
		TEXT_SIZE = fontSize;
	}
	
	private void setGlobalFont(ViewGroup root) 
	{
	    for (int i = 0; i < root.getChildCount(); i++) 
	    {
	        View child = root.getChildAt(i);
	        
	        if (child instanceof TextView)
	        {
	        	((TextView)child).setTextColor(Color.BLACK);
	        	((TextView)child).setTextSize(TEXT_SIZE);
	        }
	        else if (child instanceof EditText)
			{
	        	((EditText)child).setTextColor(Color.BLACK);
	        	((EditText)child).setTextSize(TEXT_SIZE);
			}
	        else if (child instanceof Button)
			{
	        	((Button)child).setTextColor(Color.BLACK);
	        	((Button)child).setTextSize(TEXT_SIZE);
			}
	        else if (child instanceof ViewGroup)
	        {
	        	setGlobalFont((ViewGroup)child);
	        }
	    }
	}
	
	public interface StatsCollBank
	{
		public abstract void getStats(int stats);
	}
}
